package com.all.jpa.controller;

import com.all.jpa.model.entity.SysUser;
import com.all.jpa.response.ResponseMessage;
import com.all.jpa.response.ResponseMessageFactory;
import com.all.jpa.servvice.ISysUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author LY
 * @create 2024/4/26 15:48
 * @Description 用户控制层
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private ISysUserService iSysUserService;

    /**
     * 获取所有用户信息
     * @return
     */
    @GetMapping("/findAll")
    public ResponseMessage<List<SysUser>> findAll(){
        return ResponseMessageFactory.getSuccessResponseMessage(iSysUserService.findAll());
    }

    @GetMapping("/findById")
    public ResponseMessage<SysUser> findById(String userId){
        return ResponseMessageFactory.getSuccessResponseMessage(iSysUserService.findById(Long.valueOf(userId)));
    }

    @GetMapping("/findByName")
    public ResponseMessage<List<Map<String,Object>>> findByName(String name){
        return ResponseMessageFactory.getSuccessResponseMessage(iSysUserService.findByName(name));
    }

    @GetMapping("/findByName2")
    public ResponseMessage<List<SysUser>> findByName2(String name){
        return ResponseMessageFactory.getSuccessResponseMessage(iSysUserService.findByUserName2(name));
    }

    @PostMapping("/save")
    public ResponseMessage<Void> save(){
        iSysUserService.save();
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @GetMapping("/delete")
    public ResponseMessage<Void> delete(String userId){
        iSysUserService.delete(Long.valueOf(userId));
        return ResponseMessageFactory.getSuccessResponseMessage();
    }
}
